import java.security.PublicKey;
import java.sql.DataTruncation;
import java.util.Stack;

class TreeNode{
    public char val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(char val){
        this.val = val;
    }
}
public class Test {
    //二叉树前序递归遍历实现
    public void preOrder(TreeNode root){
        if (root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //二叉树前序非递归遍历实现--引入栈
    public void preOrder1(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()){
            while(cur != null){
                System.out.print(cur.val + " ");
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }
    //二叉树的中序遍历--递归--无返回值
    public void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    //二叉树中序非递归遍历实现--引入栈
    public void inOrder1(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }
    //递归后序遍历
    public void postOrder(TreeNode root){
        if (root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    //非递归后序遍历
    public void postOrder1(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if (top.right == null || top.right == prev){
                System.out.print(top.val + " ");
                prev = top;
                stack.pop();
            }else {
                cur = top.right;
            }
        }

    }
    //给你二叉树的根节点 root ，请你采用前序遍历的方式，将二叉树转化为一个由括号和整数组成的字符串，返回构造出的字符串。
    //空节点使用一对空括号对 "()" 表示，转化后需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对

    public String tree2str(TreeNode root) {
        StringBuilder str = new StringBuilder();
        tree2strChild(root,str);
        return str.toString();
    }
    public static void tree2strChild(TreeNode root,StringBuilder stringBuilder){
        if (root == null){
            return;
        }
        stringBuilder.append(root.val);
        if (root.left != null){
            stringBuilder.append("(");
            tree2strChild(root.left,stringBuilder);
            stringBuilder.append(")");
        }else {
            if (root.right != null){
                stringBuilder.append("()");
            }else {
                return;
            }
        }
        if (root.right != null){
            stringBuilder.append("(");
            tree2strChild(root.right,stringBuilder);
            stringBuilder.append(")");
        }else {
            return;
        }
    }

    public static TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    public static void main(String[] args) {
        Test test = new Test();
        TreeNode root = createTree();
        System.out.println("递归前序遍历：");
        test.preOrder(root);
        System.out.println();
        System.out.println("非递归前序遍历：");
        test.preOrder1(root);
        System.out.println();
        System.out.println("递归中序遍历：");
        test.inOrder(root);
        System.out.println();
        System.out.println("非递归中序遍历：");
        test.inOrder1(root);
        System.out.println();
        System.out.println("递归后序遍历：");
        test.postOrder(root);
        System.out.println();
        System.out.println("非递归后序遍历：");
        test.postOrder1(root);
    }
}
